package com.example.jf.jfapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.jf.jfapp.AnnounceBlogActivity;
import com.example.jf.jfapp.DetailActivity;
import com.example.jf.jfapp.R;
import com.example.jf.jfapp.adapter.XuexiAdapter;
import com.example.jf.jfapp.bean.request.BlogRequest;
import com.example.jf.jfapp.bean.response.BlogResponse;
import com.example.jf.jfapp.utils.AppConfigUtils;
import com.example.jf.jfapp.utils.Constant;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.jf.jfapp.AnnounceBlogActivity.STATU_SEND_RESULT_KEY;
import static com.example.jf.jfapp.DetailActivity.DETAILTYPE;
import static com.example.jf.jfapp.utils.OkHttpUtils.getOkHttpClient;
import static com.example.jf.jfapp.utils.RequestUtils.requestPOST;

/**
 * Created by jf on 2017/7/27.
 */

public class XuexiFragment extends Fragment {
    private static final String TAG = "XuexiFragment";
    @BindView(R.id.xuexiRv)
    RecyclerView xuexiRv;

    Unbinder unbinder;

    int type = -1;

    ArrayList<BlogResponse.ResultBean> mResultBean = new ArrayList<>();
    @BindView(R.id.fab_talk)
    FloatingActionButton fabTalk;
    @BindView(R.id.fab_issue)
    FloatingActionButton fabIssue;
    @BindView(R.id.fab_share)
    FloatingActionButton fabShare;

    private BlogRequest mBlogRequest;
    private XuexiAdapter mXuexiAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_xuexi, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        xuexiRv.setLayoutManager(new LinearLayoutManager(getActivity()));


        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_blog_header, xuexiRv, false);
        //      View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.layout_home_footer, null);
        LinearLayout talk = headView.findViewById(R.id.talk);

        LinearLayout issue = headView.findViewById(R.id.issue);

        LinearLayout share = headView.findViewById(R.id.share);

        LinearLayout collect = headView.findViewById(R.id.collect);
        talk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //  Toast.makeText(getActivity(), "gggggggggggggggggg", Toast.LENGTH_SHORT).show();
                type = 1;
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra(DETAILTYPE, type);
                startActivity(intent);

            }
        });
        issue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //  Toast.makeText(getActivity(), "gggggggggggggggggg", Toast.LENGTH_SHORT).show();
                type = 2;
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra(DETAILTYPE, type);
                startActivity(intent);
            }
        });
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //  Toast.makeText(getActivity(), "gggggggggggggggggg", Toast.LENGTH_SHORT).show();
                type = 3;
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra(DETAILTYPE, type);
                startActivity(intent);
            }
        });

        mXuexiAdapter = new XuexiAdapter(mResultBean);

        mXuexiAdapter.setHeadView(headView);

        xuexiRv.setAdapter(mXuexiAdapter);


        mBlogRequest = new BlogRequest(type, 10, 0);
        //获取用户信息
        mBlogRequest.setUserId(AppConfigUtils.getInstance().getLoginInfo(getActivity()).getObjectId());

        getXuexiData();
        //悬浮按钮获取
        final FloatingActionMenu fab = (FloatingActionMenu) view.findViewById(R.id.fab);

        fab.setClosedOnTouchOutside(true);


    }

    private void getXuexiData() {
        final Request request = requestPOST(mBlogRequest, Constant.URL.BLOG_LIST);
        OkHttpClient okHttpClient = getOkHttpClient();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "获取博客列表失败 ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String result = response.body().string();
                    Log.e(TAG, "获取博客列表成功: " + result);
                    if (result != null) {
                        BlogResponse blogResponse = new Gson().fromJson(result, BlogResponse.class);
                        mResultBean.addAll(blogResponse.getResult());
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mXuexiAdapter.notifyDataSetChanged();
                            }
                        });

                    }
                }
            }
        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.fab_talk, R.id.fab_issue, R.id.fab_share})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fab_talk:
                Intent intenttalk=new Intent(getActivity(), AnnounceBlogActivity.class);
                intenttalk.putExtra(STATU_SEND_RESULT_KEY,1);
                startActivity(intenttalk);
                break;
            case R.id.fab_issue:
                Intent intentissue=new Intent(getActivity(), AnnounceBlogActivity.class);
                intentissue.putExtra(STATU_SEND_RESULT_KEY,2);
                startActivity(intentissue);
                break;
            case R.id.fab_share:
                Intent intentshare=new Intent(getActivity(), AnnounceBlogActivity.class);
                intentshare.putExtra(STATU_SEND_RESULT_KEY,3);
                startActivity(intentshare);
                break;
        }
    }
}
